/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package thing;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.WindowEvent;

/**
 *
 * @author Tharen
 */
public class AddWorld extends Stage {
    //World Manager
    WorldManager wm = new WorldManager();
    //Panes
    AnchorPane pane = new AnchorPane();
    //Buttons
    Button add = new Button("Add");
    Button cancel = new Button("Cancel");
    //Text boxs
    TextField name = new TextField("Name");
    Label nameLbl = new Label("Name", name);
    TextField type = new TextField("Type");
    Label typeLbl = new Label("Type", type);
    TextField imagePath = new TextField("Image Path Here");
    Label imLbl = new Label("Image Path", imagePath);
    TextField worldPath = new TextField("World Path Here");
    Label worldLbl = new Label("World Path", worldPath);
    TextArea description = new TextArea("Description");
    //Checkbox
    CheckBox modsRequired = new CheckBox("Mods Required");

    /**
     *
     */
    public AddWorld() {
        init();
    }

    /**Primary constructor
     *
     * @param arg0 StageStyle used to create this with certain effects.
     */
    public AddWorld(StageStyle arg0) {
        super(arg0);
        init();
    }

    /**
     * Sets window size and particulars for all the display objects.
     */
    private void init() {
        this.setScene(new Scene(pane, 500, 300));
        this.setTitle("Add a World");
        //Text boxs
        pane.getChildren().add(description);
        description.relocate(10, 85);
        pane.getChildren().add(nameLbl);
        pane.getChildren().add(typeLbl);
        typeLbl.relocate(190, 0);
        pane.getChildren().add(imLbl);
        imLbl.relocate(0, 25);
        pane.getChildren().add(worldLbl);
        worldLbl.relocate(190, 50);
        //Checkbox
        pane.getChildren().add(modsRequired);
        modsRequired.relocate(10, 60);
        //Buttons
        /**
         * TODO: Add random world name generator.
         */
        pane.getChildren().add(add);
        add.relocate(440, 250);
        pane.getChildren().add(cancel);
        cancel.relocate(10, 250);
        this.setResizable(false);
        //Button Actions
        add.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent arg0) {
                if (!name.getText().isEmpty() || !name.getText().contains(" ")) {
                    wm.addWorld(name.getText(), type.getText(), modsRequired.isSelected(), imagePath.getText(), description.getText(), worldPath.getText());
                } else {
                    AppLauncher.toUser("Add world failed!");
                }
            }
        });
        cancel.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent arg0) {
                hide();
            }
        });
        //Hide Action
        this.setOnHiding(new EventHandler<WindowEvent>() {

            @Override
            public void handle(WindowEvent arg0) {
                WorldManager.saveWorldValues();
            }
        });
    }
}
